package com.congee02.aop;

import com.congee02.aop.proxy.dynamic.DynamicProxyFactory;
import com.congee02.aop.proxy.dynamic.FlexibleDynamicProxyFactory;
import com.congee02.aop.service.UserService;
import com.congee02.aop.service.UserServiceImpl;

/**
 * @author congee(congee02 @ 163.com)
 * @date 1/26/2023 12:38 PM
 */
public class FlexibleDynamicProxyFactoryTest {

    public static void main(String[] args) {

        final UserService service = new UserServiceImpl();
        DynamicProxyFactory factory = null;

        /* LogProxy */
        factory = new FlexibleDynamicProxyFactory(service, (proxy, method, args1) -> {
            Object returnValue = method.invoke(service, args1);
            String username = args1[0].toString();
            System.out.println("logging operation {saving user " + username + "}");
            return returnValue.toString() + "{LOGGED}";
        });
        UserService logService = (UserService) factory.getProxyInstance();
        System.out.println(logService.save("k02"));

        /* ======================================== */
        System.out.println("========================================");

        /* NotifyProxy */
        factory = new FlexibleDynamicProxyFactory(service, (proxy, method, args1) -> {
            Object returnValue = method.invoke(logService, args1);
            String username = args1[0].toString();
            System.out.println("notifying {saving user "+ username +"}");
            return returnValue.toString() + "{NOTIFIED}";
        });

        UserService logNotifyService = (UserService) factory.getProxyInstance();
        System.out.println(logNotifyService.save("congee02"));
    }

}
